import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.*;

import javax.swing.*;

import java.util.Timer;
import java.util.TimerTask;


public class Table extends JPanel implements MouseListener, ComponentListener {
	private int [][] MatrixTable;
	private int [][] MatrixTable2;
	private int MatrixTableSize;
	private int MatrixOffset;
	private int xPosition;
	private int RectangleSize;
	private int [] coords;
	private JPanel Panel;
	private boolean pozitie_gresita;
	
	JButton buton;
	JRadioButton up;
	JRadioButton left;
	JRadioButton down;
	JRadioButton right;
	ButtonGroup group;
	
	public Table(int offset)
	{
		int i,j;
		coords = new int [3];
		MatrixOffset = offset;
		MatrixTableSize = 20;
		RectangleSize = 25;
		xPosition = 590;
		coords[2] = 1;
		pozitie_gresita = false;
//		this.setSize(new Dimension(500,600));
//		this.setLocation(0, 0);
		this.addMouseListener(this);
		this.addComponentListener(this);
		
		buton = new JButton();
		buton.setText("PULA!");
		buton.setLocation(1400, 50);
		buton.setVisible(true);
		
		up = new JRadioButton("Up");
		up.setLocation(xPosition + 500, MatrixOffset);
		up.setActionCommand("up_select");
		up.setVisible(true);
		up.setSelected(true);
		
		right = new JRadioButton("Right");
		right.setActionCommand("right_select");
		right.setSelected(true);
		
		down = new JRadioButton("Down");
		down.setActionCommand("down_select");
		down.setSelected(true);
		
		left = new JRadioButton("Left");
		left.setActionCommand("left_select");
		left.setSelected(true);

		group = new ButtonGroup();
		group.add(up);
		group.add(right);
		group.add(down);
		group.add(left);
		
//		this.add(up);
//		this.add(right);
//		this.add(down);
//		this.add(left);
//		this.add(buton);
		
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		
		MatrixTable = new int [MatrixTableSize][MatrixTableSize];
		MatrixTable2 = new int [MatrixTableSize][MatrixTableSize]; 
		for (i = 0; i < MatrixTableSize; i++)
		{
			for (j = 0; j < MatrixTableSize; j++)
			{
				this.MatrixTable[i][j] = 0;
				this.MatrixTable2[i][j] = 0;
			}
		}
		
	}
	public int [][] getMatrix()
	{
		return this.MatrixTable;
	}
	
	public void setPlane(int Xcenter, int Ycenter, int direction)
	{
		pozitie_gresita = false;
		if ((Xcenter < 2 || Xcenter > 17) || (Ycenter <2 || Ycenter > 17))
		{
			System.out.println("Nu poti sa pui acolo!");
			pozitie_gresita = true;
		}
		else{
		switch(direction)
		{
		case 2:
			for (int i = Xcenter - 2; i < Xcenter + 2; i++)
			{
				if (this.MatrixTable[i][Ycenter - 2] == 1)
					pozitie_gresita = true;
			} //prima linie cu capul in jos
			
			for (int i = Xcenter - 2; i < Xcenter + 2; i++)
			{
				if (this.MatrixTable[i][Ycenter + 1] == 1)
					pozitie_gresita = true;
			} //doua linie cu capul in jos
			for (int i = Ycenter - 2; i < Ycenter + 2; i++)
			{
				if (this.MatrixTable[Xcenter][i] == 1)
					pozitie_gresita = true;
			} //linia perpendiculara pe celelalte cu capul in jos
			break;
		case 3:
			for (int i = Ycenter - 2; i < Ycenter + 2; i++)
			{
				if (this.MatrixTable[Xcenter - 1][i] == 1)
					pozitie_gresita = true;
			} //prima linie cu capul in stanga
			
			for (int i = Ycenter - 2; i < Ycenter + 2; i++)
			{
				if (this.MatrixTable[Xcenter + 2][i] == 1)
					pozitie_gresita = true;
			} //doua linie cu capul in stanga
			for (int i = Xcenter - 2; i < Xcenter + 2; i++)
			{
				if (this.MatrixTable[i][Ycenter] == 1)
					pozitie_gresita = true;
			} //linia perpendiculara pe celelalte cu capul in stanga
			break;
		case 0:
			for (int i = Xcenter - 2; i < Xcenter + 2; i++)
			{
				if (this.MatrixTable[i][Ycenter - 1] == 1)
					pozitie_gresita = true;
			} //prima linie cu capul in sus
			
			for (int i = Xcenter - 2; i < Xcenter + 2; i++)
			{
				if (this.MatrixTable[i][Ycenter + 2] == 1)
					pozitie_gresita = true;
			} //doua linie cu capul in sus
			for (int i = Ycenter - 2; i < Ycenter + 2; i++)
			{
				if (this.MatrixTable[Xcenter][i] == 1)
					pozitie_gresita = true;
			} //linia perpendiculara pe celelalte cu capul in sus
			break;
		case 1:
			for (int i = Ycenter - 2; i < Ycenter + 2; i++)
			{
				if (this.MatrixTable[Xcenter + 1][i] == 1)
					pozitie_gresita = true;
			} //prima linie cu capul in dreapta
			
			for (int i = Ycenter - 2; i < Ycenter + 2; i++)
			{
				if (this.MatrixTable[Xcenter - 2][i] == 1)
					pozitie_gresita = true;
			} //doua linie cu capul in stanga
			for (int i = Xcenter - 2; i < Xcenter + 2; i++)
			{
				if (this.MatrixTable[i][Ycenter] == 1)
					pozitie_gresita = true;
			} //linia perpendiculara pe celelalte cu capul in stanga
			break;
		}
		if (pozitie_gresita == false){
		switch(direction){
		case 2:
			for (int i = Xcenter - 2; i < Xcenter + 2; i++)
			{
				this.MatrixTable[i][Ycenter - 2] = 1;
			} //prima linie cu capul in jos
			
			for (int i = Xcenter - 2; i < Xcenter + 2; i++)
			{
				this.MatrixTable[i][Ycenter + 1] = 1;
			} //doua linie cu capul in jos
			for (int i = Ycenter - 2; i < Ycenter + 2; i++)
			{
				this.MatrixTable[Xcenter][i] = 1;
			} //linia perpendiculara pe celelalte cu capul in jos
			break;
		case 3:
			for (int i = Ycenter - 2; i < Ycenter + 2; i++)
			{
				this.MatrixTable[Xcenter - 1][i] = 1;
			} //prima linie cu capul in stanga
			
			for (int i = Ycenter - 2; i < Ycenter + 2; i++)
			{
				this.MatrixTable[Xcenter + 2][i] = 1;
			} //doua linie cu capul in stanga
			for (int i = Xcenter - 2; i < Xcenter + 2; i++)
			{
				this.MatrixTable[i][Ycenter] = 1;
			} //linia perpendiculara pe celelalte cu capul in stanga
			break;
		case 0:
			for (int i = Xcenter - 2; i < Xcenter + 2; i++)
			{
				this.MatrixTable[i][Ycenter - 1] = 1;
			} //prima linie cu capul in sus
			
			for (int i = Xcenter - 2; i < Xcenter + 2; i++)
			{
				this.MatrixTable[i][Ycenter + 2] = 1;
			} //doua linie cu capul in sus
			for (int i = Ycenter - 2; i < Ycenter + 2; i++)
			{
				this.MatrixTable[Xcenter][i] = 1;
			} //linia perpendiculara pe celelalte cu capul in sus
			break;
		case 1:
			for (int i = Ycenter - 2; i < Ycenter + 2; i++)
			{
				this.MatrixTable[Xcenter + 1][i] = 1;
			} //prima linie cu capul in dreapt
			
			for (int i = Ycenter - 2; i < Ycenter + 2; i++)
			{
				this.MatrixTable[Xcenter - 2][i] = 1;
			} //doua linie cu capul in stanga
			for (int i = Xcenter - 2; i < Xcenter + 2; i++)
			{
				this.MatrixTable[i][Ycenter] = 1;
			} //linia perpendiculara pe celelalte cu capul in stanga
			break;
		}
		}
		}
			
	}
	
	public void setHit(int x, int y, int offset)
	{
		if (offset > xPosition)
		{
			System.out.println("x: " + x + " y: " + y);
			this.MatrixTable2[x][y] = 1;			
		}
		else
		{
			if (offset < xPosition)
			{			
				if (this.MatrixTable[x][y] == 2)
				{
					//change color
					this.MatrixTable[x][y] =1;
				}
				else
				{
					this.MatrixTable[x][y] = 1;
				}
			}
		}
	}
	public void receiveHit(int x, int y, int offset)
	{
		
	}
	
	public void drawTable()
	{
		
	}
	
	public void paint(Graphics g)
	{
		Graphics2D g2d = (Graphics2D)g;
		int i,j;
		if (coords[2] == 1)
		{
			coords[2] = 0;
			System.out.println("paint");
			for (i = 0; i < MatrixTableSize; i++ )
			{
				for (j = 0; j < MatrixTableSize; j++)
				{
					if (this.MatrixTable[i][j] == 1)
					{
					g2d.setColor(Color.BLUE);
					//g.fillRect(20 + i * 50, 20 + j * 50, 50, 50);
//						System.out.println(MatrixTable[i][j]);
						System.out.println("i: " + i + "j: " + j);
					 g2d.fillRect(MatrixOffset + 3 + i * RectangleSize, MatrixOffset + 3 + j * RectangleSize, 13, 13);
					}
					else
					{
						g2d.setColor(Color.BLACK);
						g2d.drawRect(MatrixOffset + i * RectangleSize, MatrixOffset + j * RectangleSize, RectangleSize, RectangleSize);
					}
			}
		}
			for (i = 0; i < MatrixTableSize; i++ )
			{
				for (j = 0; j < MatrixTableSize; j++)
				{
					if (this.MatrixTable2[i][j] == 1)
					{
					g2d.setColor(Color.BLUE);
					//g.fillRect(20 + i * 50, 20 + j * 50, 50, 50);
//						System.out.println(MatrixTable[i][j]);
						System.out.println("i: " + i + "j: " + j);
					 g2d.fillRect(xPosition + 3 + i * RectangleSize, MatrixOffset + 3 + j * RectangleSize, 13, 13);
					}
					else
					{
						g2d.setColor(Color.BLACK);
						g2d.drawRect(xPosition + i * RectangleSize, MatrixOffset + j * RectangleSize, RectangleSize, RectangleSize);
					}
			}
		}
	}
}
	
	public static void main(String[] args) 
	{
		// TODO Auto-generated method stub
		JFrame frame = new JFrame();
//		GridLayout layout1 = new GridLayout(10,10,10,10);
//		frame.setLayout(new BorderLayout());
		frame.setSize(1500, 700);
//		frame.setBounds(0, 0, 1500, 700);
		frame.setVisible(true);
		//frame.setResizable(false);
		Table tab1 = new Table(20);
		JButton buton;
		buton = new JButton();
		buton.setText("UNU!");
		buton.setSize(100, 100);
		buton.setLocation(100, 100);
		buton.setVisible(true);
		
		JButton buton2;
		buton2 = new JButton();
		buton2.setText("DOI");
		buton2.setSize(100, 100);
//		buton.setLocation(100, 100);
		buton2.setVisible(true);
//		frame.add(tab1);
		
//		GridLayout layout = new GridLayout(20,20,10,10);
		
		JPanel panel2 = new JPanel();
		panel2.setLayout(new BorderLayout());
//		panel2.setLocation(300, 20);
//		panel2.setPreferredSize(new Dimension(1300, 700));
//		panel2.add(buton);
		
		JPanel panel3 = new JPanel();
		panel3.setLayout(new BorderLayout());
//		panel3.setLocation(300, 20);
		panel3.setPreferredSize(new Dimension(200, 200));
//		panel3.add(buton);
		JPanel flowPanel2 = new JPanel(new FlowLayout());
		flowPanel2.add(buton);
		panel3.add(BorderLayout.CENTER, flowPanel2);
		
		JPanel panel4 = new JPanel();
		panel4.setLayout(new BorderLayout());
//		panel4.setLocation(300, 20);
//		panel4.setSize(300, 300);
		panel4.add(buton2);
		JPanel flowPanel = new JPanel(new FlowLayout());
		flowPanel.add(buton2);
		panel4.add(BorderLayout.SOUTH, flowPanel);
		
		JPanel Panel_patratele = new JPanel();
		Panel_patratele.add(tab1);
		
		JSplitPane splitpane = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT);
		splitpane.setResizeWeight(0.8);
		splitpane.setDividerLocation(0.8);
		splitpane.setRightComponent(panel3);
		splitpane.setLeftComponent(tab1);
		panel2.add(splitpane, BorderLayout.CENTER);
//		frame.add(tab1);
//		frame.add(Panel_patratele);
		frame.add(panel2);
//		pan.addMouseListener(tab1);
//		tab1.drawTable();
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
//		if ((e.getX() < 569) && (e.getY() < 569))
		{
			coords[0] = (e.getX() - MatrixOffset) / RectangleSize;
			coords[1] = (e.getY() - MatrixOffset) / RectangleSize;
			System.out.println(e.getX() + " " + e.getY());
			this.setHit(coords[0], coords[1], e.getX());
			coords[2] = 1;
			this.repaint();			
		}
		if ((e.getX() < 1129) && (e.getY() < 569))
		{
			coords[0] = (e.getX() - xPosition) / RectangleSize;
			coords[1] = (e.getY() - MatrixOffset) / RectangleSize;
			System.out.println(e.getX() + " " + e.getY());
			this.setHit(coords[0], coords[1], e.getX());
			coords[2] = 1;
			this.repaint();			
		}
	}
	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		this.repaint();
	}
	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		this.repaint();
	}
	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		this.repaint();
	}
	@Override
	public void componentHidden(ComponentEvent e) {
		// TODO Auto-generated method stub
		this.repaint();
	}
	@Override
	public void componentMoved(ComponentEvent e) {
		// TODO Auto-generated method stub
		this.repaint();
		
	}
	@Override
	public void componentResized(ComponentEvent e) {
		// TODO Auto-generated method stub
		this.repaint();
	}
	@Override
	public void componentShown(ComponentEvent e) {
		// TODO Auto-generated method stub
		
	}

}
